<?php
namespace app\agent\controller;

use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\admin\IAdminRoleCache;
use app\common\cache\agent\IAgentAdminRoleCache;
use app\common\cache\agent\IAgentCache;
use app\common\cache\bank\IBankCache;
use app\common\cache\car\ICarBrandCache;
use app\common\cache\car\ICarConfigCache;
use app\common\cache\car\ICarModelCache;
use app\common\cache\common\IAreaCache;
use app\common\controller\AgentBaseController;
use app\common\enum\AdminSuper;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsAccumulationFund;
use app\common\enum\IsAssure;
use app\common\enum\IsCar;
use app\common\enum\IsCompany;
use app\common\enum\IsCreditCard;
use app\common\enum\IsDelete;
use app\common\enum\IsHouse;
use app\common\enum\IsLoan;
use app\common\enum\IsMarry;
use app\common\enum\IsPolicy;
use app\common\enum\IsReal;
use app\common\enum\IsShop;
use app\common\enum\IsSocial;
use app\common\enum\IsStudent;
use app\common\enum\IsWhite;
use app\common\enum\PoolClaimStatus;
use app\common\enum\PoolLevel;
use app\common\enum\PoolVieStatus;
use app\common\enum\Source;
use app\common\enum\UserIntentionLevel;
use app\common\ext\IAgentTask;
use app\common\ext\IConfig;
use app\common\ext\IDb;
use app\common\ext\IIdentity;
use app\common\ext\IJuHe;
use app\common\ext\IRequest;
use think\Db;

class Admin extends AgentBaseController{
    // 默认权限
    private $default_menu = [1,2,3,4,10,11,12,13];
    private $default_node = [1,2,3,148,149];

    /**
     * @功能：编辑管理员信息
     * @开发者：cxl
     */
    public function agent_admin_upd(){
        //设置查询条件
        $agent_admin_id = get_login_agent_admin_id();

        //取得管理员详情
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);

        //页面赋值
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：管理员信息提交
     * @开发者：cxl
     */
    public function agent_admin_upd_submit(){
        //接收参数
        $agent_admin_id = get_login_agent_admin_id();
        $real = IRequest::get('real',IRequest::NOT_EMPTY,"请输入真实姓名！");
        $avatar = IRequest::get('avatar');

        //修改管理员信息
        $agent_admin_where['agent_admin_id'] = $agent_admin_id;
        $agent_admin_data['real'] = $real;
        $agent_admin_data['avatar'] = $avatar;
        $agent_admin_upd = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->setDbData($agent_admin_data)->upd();
        if($agent_admin_upd === false){
            error("修改管理员信息失败！");
        }

        //清除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //画面跳转
        $this->success("操作成功！",Url("agent_admin_upd"));
    }

    /**
     * @功能：修改密码
     * @开发者：cxl
     */
    public function agent_admin_pwd(){
        //设置查询条件
        $agent_admin_id = get_login_agent_admin_id();

        //取得管理员详情
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);

        //页面赋值
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：保存密码
     * 开发者：cxl
     */
    public function agent_admin_pwd_submit(){
        //接收参数
        $agent_admin_id = get_login_agent_admin_id();
        $old_pwd = IRequest::get("old_pwd",IRequest::NOT_EMPTY,"请输入旧密码！");
        $new_pwd = IRequest::get("new_pwd",IRequest::NOT_EMPTY,"请输入新密码！");

        //取得管理员详情
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);
        if(empty($agent_admin_info)){
            error("取得登录用户信息失败！");
        }

        //判断旧密码
        $old_pwd = md5(md5($old_pwd));
        if($agent_admin_info['pwd'] != $old_pwd){
            error("您输入的旧密码错误！");
        }

        //修改管理员信息
        $agent_admin_where['agent_admin_id'] = $agent_admin_id;
        $agent_admin_data['pwd'] = md5(md5($new_pwd));
        $agent_admin_upd = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->setDbData($agent_admin_data)->upd();
        if($agent_admin_upd === false){
            error("修改密码失败！");
        }

        //清除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //设置日志
        IAgentTask::AgentMessage($agent_admin_id,IAgentTask::PasswordUpd);

        //画面跳转
        $this->success("操作成功！",Url("agent_admin_pwd"));
    }

    /**
     * @功能：管理员列表
     * @开发者：cxl
     */
    public function agent_admin_list(){
        //接收参数
        $role_id = IRequest::get('role_id');
        $admin_super = IRequest::get('admin_super');
        $real_name = IRequest::get('real_name');
        $phone = IRequest::get('phone');

        //取得管理员数据
        $where['an.agent_id'] = get_login_agent_id();
        if(!empty($role_id)) $where["an.admin_role_id"] = array("eq",$role_id);
        if(!empty($admin_super)) $where["an.admin_super"] = array("eq",$admin_super);
        if(!empty($real_name)) $where["an.real_name"] = array("like","%{$real_name}%");
        if(!empty($phone)) $where["an.phone"] = array("like","%{$phone}%");

        //取得数据
        $agent_admin_list = IDb::getInstance("agent_admin as an")
            ->setDbFiled("an.*,ar.role_name,an.admin_super")
            ->setDbJoin("agent_admin_role as ar","an.admin_role_id=ar.admin_role_id","left")
            ->setDbWhere($where)
            ->pag();

        //整理数据
        if(!empty($agent_admin_list) && count($agent_admin_list) > 0){
            foreach($agent_admin_list as &$data){
                $data['token'] = md5(md5(self::ToKenKey).$data['agent_admin_id']);
            }
        }

        //取得管理员数据
        $role_list = IDb::getInstance("agent_admin_role")->sel();

        //模板渲染
        $this->assign('role_id',$role_id);
        $this->assign('admin_super',$admin_super);
        $this->assign('real_name',$real_name);
        $this->assign('phone',$phone);
        $this->assign('agent_admin_list',$agent_admin_list);
        $this->assign('role_list',$role_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑管理员
     * @开发者：cxl
     */
    public function agent_admin_add(){
        //取得管理员数据
        $role_list = IDb::getInstance("agent_admin_role")->sel();

        //取得默认密码
        $agent_admin_password = IConfig::getValue('AdminDefaultPassword');

        //模板渲染
        $this->assign('agent_admin_password', $agent_admin_password);
        $this->assign('role_list', $role_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑管理员
     * @开发者：cxl
     */
    public function agent_admin_edit(){
        //接收参数
        $agent_id       = get_login_agent_id();
        $agent_admin_id = IRequest::get('agent_admin_id',IRequest::NOT_EMPTY,"管理员编号不能为空！");

        //取得角色数据
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);
        if(empty($agent_admin_info['agent_admin_id'])){
            error("管理员不存在！");
        }

        //取得管理员数据
        $where = 'agent_id is null or agent_id = ' . $agent_id;
        $role_list = IDb::getInstance("agent_admin_role")->setDbWhere($where)->sel();

        //模板渲染
        $this->assign('agent_admin_id',$agent_admin_id);
        $this->assign('role_list',$role_list);
        $this->assign('agent_admin_info',$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：管理员保存
     * 开发者：cxl
     */
    public function agent_admin_submit(){
        //接收参数
        $agent_id = get_login_agent_id();
        $agent_admin_id = IRequest::get("agent_admin_id");
        $real_name = IRequest::get("real_name",IRequest::NOT_EMPTY,"真实姓名不能为空！");
        $admin_type = IRequest::get("admin_type",IRequest::NOT_EMPTY,"请选择角色！");
        $admin_role_id= IRequest::get("admin_role_id",IRequest::NOT_EMPTY,"请选择角色！");
        $avatar = IRequest::get("avatar");
        $remark = IRequest::get("remark");

        //取得角色类型
        $role_info = IAgentAdminRoleCache::getInfo($admin_role_id);
        if(empty($role_info)){
            error("取得角色信息失败！");
        }

        //实例化对象
        IDb::dbStartTrans();

        //判断用户是否已经被注册了
        if(empty($agent_admin_id)){
            //接收参数
            $phone = IRequest::get("phone",IRequest::NOT_EMPTY,"手机不能为空！");

            //判断用户名称是否被使用了
            $agent_admin_where['phone'] = $phone;
            $agent_admin_info = IDb::getInstance("agent_admin")->setDbWhere($agent_admin_where)->row();
            if(!empty($agent_admin_info)){
                error("手机号码已经被使用了！");
            }

            //获取后台用户默认密码
            $agent_admin_password = IConfig::getValue('AdminDefaultPassword');
            if(empty($agent_admin_password)){
                error('未配置后台用户密码,请联系管理员！');
            }

            //新增保存
            $agent_admin_data['admin_role_id']=$admin_role_id;
            $agent_admin_data['agent_id']=$agent_id;
            $agent_admin_data['admin_type']=$admin_type;
            $agent_admin_data['admin_super']=$role_info['admin_super'];
            $agent_admin_data['phone']=$phone;
            $agent_admin_data['avatar']=$avatar;
            $agent_admin_data['real_name']=$real_name;
            $agent_admin_data['remark']=$remark;
            $agent_admin_data['pwd'] = md5(md5($agent_admin_password));
            $agent_admin_data['login_count'] = 0;
            $agent_admin_data['reg_ip']=get_client_ip();
            $agent_admin_data['reg_time']=get_date_time();
            $agent_admin_data['login_count'] = 0;
            $agent_admin_id = IDb::getInstance("agent_admin")->setDbData($agent_admin_data)->add();
            if($agent_admin_id === false){
                IDb::dbRollback();
                error("创建管理员失败！");
            }
        }else {
            //修改保存
            $agent_admin_where = null;
            $agent_admin_where['agent_admin_id'] = $agent_admin_id;
            $agent_admin_data['admin_role_id']=$admin_role_id;
            $agent_admin_data['admin_type']=$admin_type;
            $agent_admin_data['admin_super']=$role_info['admin_super'];
            $agent_admin_data['avatar']=$avatar;
            $agent_admin_data['real_name']=$real_name;
            $agent_admin_data['remark']=$remark;
            $agent_admin_upd = IDb::getInstance("agent_admin")->setDbWhere($agent_admin_where)->setDbData($agent_admin_data)->upd();
            if ($agent_admin_upd === false) {
                IDb::dbRollback();
                error("编辑管理员信息失败！");
            }
        }

        //提交事物处理
        IDb::dbCommit();

        //清除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //画面跳转
        $this->redirect("agent_admin_list");
    }

    /**
     * @功能：编辑管理员
     * @开发者：cxl
     */
    public function agent_admin_info(){
        //接收参数
        $agent_admin_id = IRequest::get('agent_admin_id',IRequest::NOT_EMPTY,"管理员编号不能为空！");
        $token = IRequest::get("token",IRequest::NOT_EMPTY,"您无权查看此用户信息！");

        //检测请求的合法信息
        $token_key = md5(md5(self::ToKenKey).$agent_admin_id);
        if($token != $token_key){
            error("您无权查看此用户信息！");
        }

        //取得角色数据
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);

        //取得角色名称
        if(!empty($agent_admin_info['admin_role_id'])){
            $agent_role_name = IAgentAdminRoleCache::getInfo($agent_admin_info['admin_role_id'],"role_name");
        }

        //判断角色名称是否存在
        if(empty($agent_role_name)){
            $agent_role_name = "--";
        }

        //设置layout
        $this->layout("layout_empty");

        //模板渲染
        $this->assign('agent_admin_id',$agent_admin_id);
        $this->assign('agent_role_name',$agent_role_name);
        $this->assign('agent_admin_info',$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：删除管理员
     * @开发者：cxl
     */
    public function agent_admin_del(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"管理员编号不能为空！");

        //取得角色使用信息
        $agent_admin_where["agent_admin_id"] = $agent_admin_id;
        $agent_admin_data['is_delete'] = IsDelete::Yes;
        $agent_admin_data['update_admin']=get_login_agent_admin_id();
        $agent_admin_data['update_time']=get_date_time();
        $agent_admin_upd = IDb::getInstance("agent_admin")->setDbWhere($agent_admin_where)->setDbData($agent_admin_data)->upd();
        if($agent_admin_upd === false){
            error("修改管理员信息失败！");
        }

        //清除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //画面跳转
        $this->redirect("agent_admin_list");
    }

    /**
     * @功能：还原管理员
     * @开发者：cxl
     */
    public function agent_admin_reset(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"管理员编号不能为空！");

        //取得角色使用信息
        $agent_admin_where["agent_admin_id"] = $agent_admin_id;
        $agent_admin_data['is_delete'] = IsDelete::No;
        $agent_admin_data['update_admin']=get_login_agent_admin_id();
        $agent_admin_data['update_time']=get_date_time();
        $agent_admin_upd = IDb::getInstance("agent_admin")->setDbWhere($agent_admin_where)->setDbData($agent_admin_data)->upd();
        if($agent_admin_upd === false){
            error("修改管理员信息失败！");
        }

        //清除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //画面跳转
        $this->redirect("agent_admin_list");
    }

    /**
     * @功能：重置密码
     * @开发者：cxl
     */
    public function agent_admin_reset_pwd(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"管理员编号不能为空！");

        //获取后台用户默认密码
        $agent_admin_password = IConfig::getValue('AdminDefaultPassword');
        if(empty($agent_admin_password)){
            error('未配置后台用户密码,请联系管理员！');
        }

        //取得角色使用信息
        $agent_admin_where["agent_admin_id"] = $agent_admin_id;
        $agent_admin_data['pwd'] = md5(md5($agent_admin_password));
        $agent_admin_data['update_admin']=get_login_agent_admin_id();
        $agent_admin_data['update_time']=get_date_time();
        $agent_admin_upd = IDb::getInstance("agent_admin")->setDbWhere($agent_admin_where)->setDbData($agent_admin_data)->upd();
        if($agent_admin_upd === false){
            error("重置密码失败！");
        }

        //清除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //充值密码
        $this->success('重置密码成功！');
    }

    /**
     * @功能：角色列表
     * @开发者：cxl
     */
    public function role_list(){
        //接收参数
        $agent_id = get_login_agent_id();
        $role_name = IRequest::get('role_name');
        $admin_super = IRequest::get('admin_super');

        //查询条件
        $agent_admin_role_where = '(agent_id is null or agent_id='.$agent_id.')';
        if(!empty($role_name)) $agent_admin_role_where .=" and role_name like  '%$role_name%'";
        if(!empty($admin_super)) $agent_admin_role_where .=' and admin_super  = '. $admin_super ;

        //取得角色列表
        $agent_admin_role_list = IDb::getInstance("agent_admin_role")->setDbWhere($agent_admin_role_where)->pag();

        //模板渲染
        $this->assign('role_name',$role_name);
        $this->assign('agent_id',$agent_id);
        $this->assign('admin_super',$admin_super);
        $this->assign('role_list',$agent_admin_role_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑角色
     * @开发者：cxl
     */
    public function role_add(){
        //取得管理员类型
        $admin_super = get_login_agent_super();

        if($admin_super != AdminSuper::Super){
            error("只有超级管理员才有权限！");
        }

        //模板渲染
        $this->assign('admin_super',$admin_super);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑角色
     * @开发者：cxl
     */
    public function role_edit(){
        //接收参数
        $role_id = IRequest::get('role_id',IRequest::NOT_EMPTY,"角色编号不能为空！");

        //判断是否为超级管理员
        $admin_super = get_login_agent_super();
        if($admin_super != AdminSuper::Super){
            error("只有超级管理员才有权限！");
        }

        //取得角色数据
        $agent_admin_role_info = IAgentAdminRoleCache::getInfo($role_id);
        if(empty($agent_admin_role_info['admin_role_id'])){
            error("角色不存在！");
        }

        //判断是否为共通角色
        if($agent_admin_role_info['agent_id'] != get_login_agent_id()){
            error("不能编辑共同角色！");
        }

        //设置layout
        $this->layout("layout_empty");

        //模板渲染
        $this->assign('admin_super',$admin_super);
        $this->assign('role_id',$role_id);
        $this->assign('role_info',$agent_admin_role_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：删除角色
     * @开发者：cxl
     */
    public function role_del(){
        //接收参数
        $role_id = IRequest::get("role_id",IRequest::NOT_EMPTY,"角色编号不能为空！");

        //判断是否为超级管理员
        $admin_super = get_login_agent_super();
        if($admin_super != AdminSuper::Super){
            error("只有超级管理员才有权限！");
        }

        //取得角色数据
        $agent_admin_role_info = IAgentAdminRoleCache::getInfo($role_id);
        if(empty($agent_admin_role_info)){
            error("角色不存在！");
        }

        //判断是否为共通角色
        if($agent_admin_role_info['agent_id'] != get_login_agent_id()){
            error("不能删除共同角色！");
        }

        //取得角色使用信息
        $agent_admin_where['admin_role_id'] = $role_id;
        $agent_admin_info = IDb::getInstance("agent_admin")->setDbWhere($agent_admin_where)->con();

        //判断角色是否已经被使用了
        if(empty($agent_admin_info)){
            //删除角色
            $agent_admin_role_where['admin_role_id'] = $role_id;
            $agent_admin_role_del = IDb::getInstance("agent_admin_role")->setDbWhere($agent_admin_role_where)->del();
            if($agent_admin_role_del === false){
                error("删除角色失败，请联系管理员！");
            }
        }else{
            error("角色已经被{$agent_admin_info}个管理员使用了，不能删除！");
        }

        //删除缓存
        IAgentAdminRoleCache::deleteInfo($role_id);

        //画面跳转
        $this->redirect("role_list");
    }

    /**
     * @功能：角色保存
     * 开发者：cxl
     */
    public function role_submit(){
        //接收参数
        $role_id = IRequest::get("role_id");
        $role_name = IRequest::get("role_name",IRequest::NOT_EMPTY,"请输入角色名称！");
        $admin_super = IRequest::get("admin_super");
        $remark = IRequest::get("remark");

        //取得管理员类型
        $login_admin_super = get_login_agent_super();
        if($login_admin_super == AdminSuper::Ordinary){
            error("只有超级管理员才有权限！");
        }

        //判断数据提交类型
        $agent_admin_role = IDb::getInstance("agent_admin_role");

        //判断角色名是否存在
        $find_where['role_name'] = $role_name;
        if(!empty($role_id)){
            $find_where['admin_role_id'] = ['neq',$role_id];
        }
        $agent_admin_role_info=$agent_admin_role->setDbWhere($find_where)->row();
        if(!empty($agent_admin_role_info)){
            error('角色已经存在');
        }

        //保存角色信息
        if(empty($role_id)){
            //新增保存
            $agent_admin_role_data['agent_id'] = get_login_agent_id();
            $agent_admin_role_data['role_name'] = $role_name;
            $agent_admin_role_data['admin_super'] = $admin_super;
            $agent_admin_role_data['remark'] = $remark;
            $agent_admin_role_data['create_admin'] = get_login_agent_admin_id();
            $agent_admin_role_data['create_time'] = get_date_time();

            IDb::dbStartTrans();
            $agent_admin_role_add = IDb::getInstance("agent_admin_role")->setDbData($agent_admin_role_data)->add();
            if($agent_admin_role_add === false){
                IDb::dbRollback();
                error("创建角色失败！");
            }

            //添加首页，编辑我的信息，修改密码权限
            $menu = $this->default_menu;
            $node = $this->default_node;

            //添加角色权限
            $agent_access_data['admin_role_id'] = $agent_admin_role_add;
            $agent_access_data['menu'] = implode(",",$menu);
            $agent_access_data['node'] = implode(",",$node);
            $agent_admin_role_add = IDb::getInstance("agent_admin_role_menu")->setDbData($agent_access_data)->add();
            if($agent_admin_role_add === false){
                IDb::dbRollback();
                error("添加角色默认权限失败！");
            }
            IDb::dbCommit();
        }else{
            //修改保存
            $agent_admin_role_where['admin_role_id'] = $role_id;
            $agent_admin_role_where['agent_id'] = get_login_agent_id();
            $agent_admin_role_data['role_name'] = $role_name;
            $agent_admin_role_data['admin_super'] = $admin_super;
            $agent_admin_role_data['remark'] = $remark;
            $agent_admin_role_data['update_admin']=get_login_agent_admin_id();
            $agent_admin_role_data['update_time']=get_date_time();
            $agent_admin_role_upd = IDb::getInstance("agent_admin_role")->setDbWhere($agent_admin_role_where)->setDbData($agent_admin_role_data)->upd();
            if($agent_admin_role_upd === false){
                error("修改角色信息失败！");
            }
        }

        //删除缓存
        IAgentAdminRoleCache::deleteInfo($role_id);

        //画面跳转
        $this->redirect("role_list");
    }

    /**
     * @功能：角色信息
     * @开发者：cxl
     */
    public function role_info(){
        //接收参数
        $role_id = IRequest::get('role_id',IRequest::NOT_EMPTY,"角色编号不能为空！");

        //取得角色数据
        $agent_admin_role_info = IAdminRoleCache::getInfo($role_id);

        //设置layout
        $this->layout("layout_empty");

        //模板渲染
        $this->assign('role_info',$agent_admin_role_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：角色权限设置
     * @开发者：cxl
     */
    public function role_access_edit(){
        //接收参数
        $role_id= IRequest::get('role_id',IRequest::NOT_EMPTY,"角色编号不能为空！");

        //取得角色数据
        $agent_admin_role_info = IAgentAdminRoleCache::getInfo($role_id);
        if(empty($agent_admin_role_info)){
            error("角色不存在！");
        }

        //取得菜单列表
        $menu_where['is_delete'] = array("eq",1);
        $menu_list = IDb::getInstance("agent_admin_menu")
            ->setDbFiled("menu_id,name,parent_id,if(default_node is null,'N',concat('N',default_node)) as default_node")
            ->setDbWhere($menu_where)
            ->setDbOrder("order_by")
            ->sel();

        //取得菜单节点
        $menu_node_where['an.is_delete'] = IsDelete::No;
        $menu_node = IDb::getInstance("agent_admin_menu_node as amn")
            ->setDbFiled("concat('N',amn.node_id) as menu_id,concat(an.title,'【',an.model_name,'：',an.action_name,'】') as name,amn.menu_id as parent_id,an.model_name,an.action_name,'N' as default_node")
            ->setDbJoin("agent_admin_node as an","an.node_id=amn.node_id")
            ->setDbWhere($menu_node_where)
            ->sel();

        //整理数据
        $menu_list = array_merge($menu_list,$menu_node);

        //取得选中菜单
        $menu_node_map["admin_role_id"] = $role_id;
        $menu_node_info = IDb::getInstance("agent_admin_role_menu")->setDbWhere($menu_node_map)->row();

        //整理数据
        $menu = explode(",",$menu_node_info['menu']);
        $node = explode(",",$menu_node_info['node']);
        $node = array_map(function($item){return "N{$item}";},$node);

        //合并数据
        $node_list = array_merge($menu,$node);

        //模板渲染
        $this->assign('role_id',$role_id);
        $this->assign('menu_list',$menu_list);
        $this->assign('node_list',$node_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：角色权限提交
     * @开发者：cxl
     */
    public function role_access_submit(){
        //接收参数
        $role_id= IRequest::get('role_id',IRequest::NOT_EMPTY,"角色编号不能为空！");
        $data= IRequest::get('data/a');

        //判断是否为超级管理员
        $admin_super = get_login_agent_super();
        if($admin_super != AdminSuper::Super){
            error("只有超级管理员才有权限！");
        }

        //取得角色数据
        $agent_admin_role_info = IAgentAdminRoleCache::getInfo($role_id);
        if(empty($agent_admin_role_info)){
            error("角色不存在！");
        }

        //整理数据 添加首页，编辑我的信息，修改密码权限
        $menu = $this->default_menu;
        $node = $this->default_node;
        if(is_array($data)){
            foreach($data as $item){
                //判断是否为节点
                if(stripos($item,"N") === false){
                    $menu[] = $item;
                }else{
                    $node[] = str_ireplace("N","",$item);
                }
            }
        }

        //判断角色是否存在
//        $role_menu_info = IAgentAdminRoleCache::getInfo($role_id);
        $role_menu_info = IDb::getInstance("agent_admin_role_menu")->setDbWhere(['admin_role_id'=>$role_id])->row();
        if(empty($role_menu_info)){
            //添加角色权限
            $agent_admin_role_data['admin_role_id'] = $role_id;
            $agent_admin_role_data['menu'] = implode(",",$menu);
            $agent_admin_role_data['node'] = implode(",",$node);
            $agent_admin_role_add = IDb::getInstance("agent_admin_role_menu")->setDbData($agent_admin_role_data)->add();
            if($agent_admin_role_add === false){
                error("修改角色权限失败1！");
            }
        }else{
            //修改角色权限
            $agent_admin_role_data['menu'] = implode(",",$menu);
            $agent_admin_role_data['node'] = implode(",",$node);
            $agent_admin_role_upd = IDb::getInstance("agent_admin_role_menu")->setDbWhere("admin_role_id='{$role_id}'")->setDbData($agent_admin_role_data)->upd();
            if($agent_admin_role_upd === false){
                error("修改角色权限失败2！");
            }
        }

        //画面跳转
        $this->redirect("role_list");
    }

    /**
     * @功能：握旗客户材料录入
     * @开发者：cxl
     */
    public function user_add(){
        //接收参数
        $agent_id = get_login_agent_id();

        //判断提交方式
        if(request()->isPost()){
            $type = IRequest::get('type');
            $code = 0;
            $msg = '';
            $data = [];
            if($type == 'phone'){
                //检查手机重复
                $phone = IRequest::get('phone',IRequest::NOT_EMPTY,"手机号码不能为空！");
                $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['phone'=>$phone])->row();
                if($is_exist){
                    $code = 1;
                    $msg = '手机号码已存在！';
                }
            }else if($type == 'real'){
                //实名验证
                $real= IRequest::get('real',IRequest::NOT_EMPTY,"姓名不能为空！");
                $identity= IRequest::get('identity',IRequest::NOT_EMPTY,"身份证号码不能为空！");
                $identity = trim($identity);

                //检查身份证号重复
                $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['identity'=>$identity])->row();
                if($is_exist){
                    $code = 1;
                    $msg = "身份证号码已存在！";
                }else if(IJuHe::real($real,$identity)){
                    $code = 0;
                    $data['user_info'] = IIdentity::getInfo($identity);
                }else{
                    $code = 1;
                    $msg = '实名未通过';
                }
            }else if($type == 'car'){
                //实名车辆型号、配置
                $field= IRequest::get('field',IRequest::NOT_EMPTY,"姓名不能为空！");
                $id= IRequest::get('id');
                if($field == 'brand'){
                    $data['data'] = ICarBrandCache::getList();
                }else if($field == 'model'){
                    $data['data'] = ICarModelCache::getList($id);
                }else if($field == 'config'){
                    $data['data'] = ICarConfigCache::getList(null,$id);
                }
            }

            return $this->return_json($code,$msg,$data);
        }

        //取得分公司信息
        $agent_info = IAgentCache::getInfo($agent_id);

        //取得区域信息
        $area_list = IAreaCache::getAllList(2);
        $location_list = IAreaCache::getAllList(3);

        //取得发卡银行
        $bank_list = IBankCache::getList();

        //模板渲染
        $this->assign('area_list',$area_list);
        $this->assign('location_list',$location_list);
        $this->assign('bank_list',$bank_list);
        $this->assign('area',array($agent_info['province_id'],$agent_info['city_id']));

        //渲染模板输出
        return view();
    }

    /**
     * @功能：握旗客户材料录入提交
     * @开发者：gys
     */
    public function user_add_submit(){
        $user_data['real'] = IRequest::get('real',IRequest::NOT_EMPTY,"姓名不能为空！");
        $user_data['occupation'] = IRequest::get('occupation',IRequest::NOT_EMPTY,"职业不能为空！");
        $user_data['is_social']= IRequest::get('is_social',IRequest::NOT_EMPTY,"是否有社保不能为空！");
        $user_data['is_accumulation_fund'] = IRequest::get('is_accumulation_fund',IRequest::NOT_EMPTY,"是否有公积金不能为空！");
        $user_data['is_assure'] = IRequest::get('is_assure',IRequest::NOT_EMPTY,"是否有过担保不能为空！");
        $user_data['is_white'] = IRequest::get('is_white',IRequest::NOT_EMPTY,"是否黑户不能为空！");
        $user_data['is_overdue'] = IRequest::get('is_overdue',IRequest::NOT_EMPTY,"是否当前逾期不能为空！");
        $user_data['is_court'] = IRequest::get('is_court',IRequest::NOT_EMPTY,"是否有法院执行记录不能为空！");
        $user_data['phone'] = IRequest::get('phone',IRequest::NOT_EMPTY,"手机号码不能为空！");
        $user_data['birth_address'] = IRequest::get('birth_address');
        $user_data['domicile_address'] = IRequest::get('domicile_address');
        $user_data['work_address'] = IRequest::get('work_address');
        $user_data['is_marry'] = IRequest::get('is_marry',IRequest::NOT_EMPTY,"婚姻状况不能为空！");
        $user_data['is_student'] = IRequest::get('is_student',IRequest::NOT_EMPTY,"是否是学生不能为空！");
        $user_data['is_real'] = IRequest::get('is_real');
        $user_data['intention_level'] = IRequest::get('intention_level',IRequest::NOT_EMPTY,"意向等级不能为空！");

        $identity = IRequest::get('identity');
        $sex = IRequest::get('sex');
        $age = IRequest::get('age');
        $voice_url = IRequest::get('voice_url');
        $proxy_url = IRequest::get('proxy_url');

        //取得其他参数
        $claim_data['money_start']= IRequest::get('min_loan',IRequest::NOT_EMPTY,"最小意向贷款不能为空！");
        $claim_data['money_end']= IRequest::get('max_loan',IRequest::NOT_EMPTY,"最大意向贷款不能为空！");
        $claim_data['period']= IRequest::get('period',IRequest::NOT_EMPTY,"周期不能为空！");
        $claim_data['user_note']= IRequest::get('user_note');

        $claim_data['money_start'] = floatval($claim_data['money_start']);
        $claim_data['money_end'] = floatval($claim_data['money_end']);
        $claim_data['period'] = intval($claim_data['period']);
        if($claim_data['period'] <= 0){
            error("贷款周期必须大于0！");
        }
        if(bccomp($claim_data['money_start'],$claim_data['money_end']) > 0){
            error("最小意向贷款不能大于最大意向贷款！");
        }

        if(!empty($voice_url)) $user_data['voice_url'] = $voice_url;
        if(!empty($proxy_url)) $user_data['proxy_url'] = $proxy_url;

        if(!empty($sex)) $user_data['sex'] = $sex;
        if(!empty($sex)) $user_data['sex'] = $sex;
        if(!empty($age)) $user_data['age'] = $age;
        if(!empty($identity)){
            $identity = trim($identity);
            $user_data['identity'] = $identity;

            //检查身份证号重复
            $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['identity'=>$user_data['identity']])->row();
            if($is_exist){
                //提示错误信息
                error("身份证号码已存在！");
            }

            //判断是否实名
            if($user_data['is_real'] != IsReal::yes){
                if(IJuHe::real($user_data['real'],$user_data['identity'])){
                    //实名成功，获取身份证号中的性别、年龄
                    $identity_info = IIdentity::getInfo($user_data['identity']);
                    $user_data['sex'] = $identity_info['sex'];
                    $user_data['age'] = $identity_info['age'];
                    //设置为已实名认证
                    $user_data['is_real'] = IsReal::yes;
                }else{
                    $user_data['is_real'] = IsReal::not;
                    error('实名认证失败，请检查姓名与身份证是否匹配');
                }
            }
        }else{
            $user_data['is_real'] = IsReal::not;
        }

        if($user_data['is_marry'] == IsMarry::married){
            //已婚
            $user_data['couple_name'] = IRequest::get('couple_name');
            $user_data['couple_phone'] = IRequest::get('couple_phone');
            $user_data['couple_identity'] = IRequest::get('couple_identity');
            $user_data['children_num'] = IRequest::get('children_num');
        }elseif ($user_data['is_marry'] == IsMarry::divorce){
            //离异
            $user_data['children_num'] = IRequest::get('children_num');
        }

        //职业值转化为字符串
        $user_data['occupation'] = HtmlEnumValue::$enum_value['identity'][$user_data['occupation']];

        //接收贷款数据
        $user_data['is_loan']= IRequest::get('is_loan',IRequest::NOT_EMPTY,"是否有贷款不能为空！");
        if($user_data['is_loan'] == IsLoan::Yes){
            $loan_data = IRequest::get('loan');
        }else{
            $loan_data = null;
        }

        //接收贷款数据
        $user_data['is_assure']= IRequest::get('is_assure',IRequest::NOT_EMPTY,"是否有担保不能为空！");
        if($user_data['is_assure'] == IsAssure::Yes){
            $assure_data = IRequest::get('assure');
        }else{
            $assure_data = null;
        }

        //接收公司数据
        $user_data['is_company']= IRequest::get('is_company',IRequest::NOT_EMPTY,"是否有公司不能为空！");
        if($user_data['is_company'] == IsCompany::Yes){
            $company_data = IRequest::get('company');
            if(empty($company_data['company_name'])){
                $company_data = null;
            }
        }else{
            $company_data = null;
        }

        //接收保单数据
        $user_data['is_policy']= IRequest::get('is_policy',IRequest::NOT_EMPTY,"是否有保单不能为空！");
        if($user_data['is_policy'] == IsPolicy::Yes){
            $policy_data = IRequest::get('policy');
        }else{
            $policy_data = null;
        }

        //接收信用卡数据
        $user_data['is_credit_card']= IRequest::get('is_card',IRequest::NOT_EMPTY,"是否有信用卡不能为空！");
        if($user_data['is_credit_card'] == IsCreditCard::Yes){
            $card_data = IRequest::get('card');
        }else{
            $card_data = null;
        }

        //接收房产数据
        $user_data['is_house']= IRequest::get('is_house',IRequest::NOT_EMPTY,"是否有房不能为空！");
        if($user_data['is_house'] == IsHouse::Yes){
            $house_data = IRequest::get('house');
        }else{
            $house_data = null;
        }

        //接收车辆数据
        $user_data['is_car']= IRequest::get('is_car',IRequest::NOT_EMPTY,"是否有车不能为空！");
        if($user_data['is_car'] == IsHouse::Yes){
            $car_data = IRequest::get('car');
        }else{
            $car_data = null;
        }

        //接收网店数据
        $user_data['is_shop']= IRequest::get('is_shop',IRequest::NOT_EMPTY,"是否有网店不能为空！");
        if($user_data['is_shop'] == IsShop::Yes){
            $shop_data = IRequest::get('shop');
        }else{
            $shop_data = null;
        }

        //计算资料完善程度
        $material = (intval(count($user_data) / 16) * 100);
        if($material < 60){
            $material = rand(60,70);
        }else if($material > 100){
            $material = 100;
        }

        //判断用户级别
        if($user_data['is_white'] == IsWhite::No || $user_data['is_student'] == IsStudent::Yes){ //学生和黑户强制设置为C类客户
            $pool_level = PoolLevel::C;
        }else{
            //判断是否有车、有房或者有保单
            if($user_data['is_house'] == IsHouse::Yes || ($user_data['is_policy'] == IsPolicy::Yes && $user_data['is_car'] == IsCar::Yes)){
                $pool_level = PoolLevel::A;//A类客户
            }else{
                //判断是否有信用卡、社保、公积金、公司
                if($user_data['is_policy'] == IsPolicy::Yes || $user_data['is_car'] == IsCar::Yes || $user_data['is_credit_card'] == IsCreditCard::Yes || $user_data['is_social'] == IsSocial::Yes || $user_data['is_company'] == IsCompany::Yes  || $user_data['is_accumulation_fund'] == IsAccumulationFund::Yes){
                    $pool_level = PoolLevel::B;//B类客户
                }else{
                    $pool_level = PoolLevel::C;//C类客户
                }
            }
        }

        //判断用户等级
        $claim_data['material']= $material; // 资料完善程度（1~100）
        $claim_data['pool_level']= $pool_level; // 领取用户池等级【1、C类用户；2、B类用户；3、A类用户】

        //整理区域数据
        $area = IRequest::get('area');
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }
        if(empty($province_id) || empty($city_id)){
            error("请选择客户所在城市！");
        }

        //设置数据
        $user_data['province_id'] = $province_id;
        $user_data['city_id'] = $city_id;

        //默认数据
        $user_data['agent_id'] = get_login_agent_id();
        $user_data['agent_admin_id'] = get_login_agent_admin_id();
        $user_data['reg_ip'] = get_client_ip();
        $user_data['source'] = Source::AgentAdmin;

        //检查手机重复
        $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['phone'=>$user_data['phone']])->row();
        if($is_exist){
            //提示错误信息
            error("手机号码已存在！");
        }

        $user_data['nick'] = get_string_rsa($user_data['phone'], 3, 4) . "_" . rand(1000,9999);

        IDb::dbStartTrans();
        $user_id = IDb::getInstance('user')->setDbData($user_data)->add();
        if ($user_id === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("添加客户信息失败！");
        }

        //用户意向
        if(!empty($claim_data) && is_array($claim_data)){
            $claim_data['province_id'] = $province_id;
            $claim_data['city_id'] = $city_id;
            $claim_data['user_id'] = $user_id;
            if(in_array($user_data['intention_level'],[UserIntentionLevel::A,UserIntentionLevel::B])){
                $claim_data['pool_claim_status'] = PoolClaimStatus::DoNotDelivery;
            }else{
                $claim_data['pool_claim_status'] = PoolClaimStatus::NotDelivery;
            }
            $result = IDb::getInstance('user_pool_claim')->setDbData($claim_data)->add();
            if(!$result){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加贷款意向失败！");
            }
        }

        //公司信息
        if(!empty($company_data) && is_array($company_data)){
            $company_data['user_id'] = $user_id;
            $result = IDb::getInstance('user_company')->setDbData($company_data)->add();
            if(!$result){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加公司信息失败！");
            }
        }

        //贷款记录
        if(!empty($loan_data) && is_array($loan_data)){
            $result = true;
            foreach ($loan_data as $val){
                $val['user_id'] = $user_id;
                //添加贷款记录
                $result = IDb::getInstance('user_loan')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加贷款信息失败！");
            }
        }

        //担保记录
        if(!empty($assure_data) && is_array($assure_data)){
            $result = true;
            foreach ($assure_data as $val){
                $val['user_id'] = $user_id;
                //添加担保记录
                $result = IDb::getInstance('user_assure')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加担保信息失败！");
            }
        }

        //保单记录
        if(!empty($policy_data) && is_array($policy_data)){
            $result = true;
            foreach ($policy_data as $val){
                $val['user_id'] = $user_id;
                //添加保单记录
                $result = IDb::getInstance('user_policy')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加保单信息失败！");
            }
        }

        //信用卡记录
        if(!empty($card_data) && is_array($card_data)){
            $result = true;
            foreach ($card_data as $val){
                $val['user_id'] = $user_id;
                //添加信用卡记录
                $result = IDb::getInstance('user_credit_card')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加信用卡信息失败！");
            }
        }

        //房产记录
        if(!empty($house_data) && is_array($house_data)){
            $result = true;
            foreach ($house_data as $val){
                $val['user_id'] = $user_id;
                //添加房产记录
                $result = IDb::getInstance('user_house')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加房产信息失败！");
            }
        }

        //车辆记录
        if(!empty($car_data) && is_array($car_data)){
            $result = true;
            foreach ($car_data as $val){
                $val['user_id'] = $user_id;
                //添加车辆信息
                $result = IDb::getInstance('user_car')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加车辆信息失败！");
            }
        }

        //网店记录
        if(!empty($shop_data) && is_array($shop_data)){
            $result = true;
            foreach ($shop_data as $val){
                $val['user_id'] = $user_id;
                //添加网店信息
                $result = IDb::getInstance('user_shop')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加网店信息失败！");
            }
        }

        //事物提交
        IDb::dbCommit();

        //设置日志
        IAgentTask::AgentMessage(get_login_agent_admin_id(),IAgentTask::UserInfo,array(array(2,IAgentTask::ToUser,$user_id)));

        //返回信息
        return $this->return_json(0,'添加成功');
    }

    /**
     * @功能：聚企客户材料录入
     * @开发者：cxl
     */
    public function user_add_vie(){
        //接收参数
        $agent_id = get_login_agent_id();

        //判断提交方式
        if(request()->isPost()){
            $type = IRequest::get('type');
            $code = 0;
            $msg = '';
            $data = [];
            if($type == 'phone'){
                //检查手机重复
                $phone = IRequest::get('phone',IRequest::NOT_EMPTY,"手机号码不能为空！");
                $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['phone'=>$phone])->row();
                if($is_exist){
                    $code = 1;
                    $msg = '手机号码已存在！';
                }
            }else if($type == 'real'){
                //实名验证
                $real= IRequest::get('real',IRequest::NOT_EMPTY,"姓名不能为空！");
                $identity= IRequest::get('identity',IRequest::NOT_EMPTY,"身份证号码不能为空！");
                $identity = trim($identity);

                //检查身份证号重复
                $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['identity'=>$identity])->row();
                if($is_exist){
                    $code = 1;
                    $msg = "身份证号码已存在！";
                }else if(IJuHe::real($real,$identity)){
                    $code = 0;
                    $data['user_info'] = IIdentity::getInfo($identity);
                }else{
                    $code = 1;
                    $msg = '实名未通过';
                }
            }else if($type == 'car'){
                //实名车辆型号、配置
                $field= IRequest::get('field',IRequest::NOT_EMPTY,"姓名不能为空！");
                $id= IRequest::get('id');
                if($field == 'brand'){
                    $data['data'] = ICarBrandCache::getList();
                }else if($field == 'model'){
                    $data['data'] = ICarModelCache::getList($id);
                }else if($field == 'config'){
                    $data['data'] = ICarConfigCache::getList(null,$id);
                }
            }

            return $this->return_json($code,$msg,$data);
        }

        //取得分公司信息
        $agent_info = IAgentCache::getInfo($agent_id);

        //取得区域信息
        $area_list = IAreaCache::getAllList(2);
        $location_list = IAreaCache::getAllList(3);

        //取得发卡银行
        $bank_list = IBankCache::getList();

        //模板渲染
        $this->assign('area_list',$area_list);
        $this->assign('location_list',$location_list);
        $this->assign('bank_list',$bank_list);
        $this->assign('area',array($agent_info['province_id'],$agent_info['city_id']));

        //渲染模板输出
        return view();
    }

    /**
     * @功能：聚企客户材料录入提交
     * @开发者：gys
     */
    public function user_add_vie_submit(){
        $user_data['real'] = IRequest::get('real',IRequest::NOT_EMPTY,"姓名不能为空！");
        $user_data['occupation'] = IRequest::get('occupation',IRequest::NOT_EMPTY,"职业不能为空！");
        $user_data['is_social']= IRequest::get('is_social',IRequest::NOT_EMPTY,"是否有社保不能为空！");
        $user_data['is_accumulation_fund'] = IRequest::get('is_accumulation_fund',IRequest::NOT_EMPTY,"是否有公积金不能为空！");
        $user_data['is_assure'] = IRequest::get('is_assure',IRequest::NOT_EMPTY,"是否有过担保不能为空！");
        $user_data['is_white'] = IRequest::get('is_white',IRequest::NOT_EMPTY,"是否黑户不能为空！");
        $user_data['is_overdue'] = IRequest::get('is_overdue',IRequest::NOT_EMPTY,"是否当前逾期不能为空！");
        $user_data['is_court'] = IRequest::get('is_court',IRequest::NOT_EMPTY,"是否有法院执行记录不能为空！");
        $user_data['phone'] = IRequest::get('phone',IRequest::NOT_EMPTY,"手机号码不能为空！");
        $user_data['birth_address'] = IRequest::get('birth_address');
        $user_data['domicile_address'] = IRequest::get('domicile_address');
        $user_data['work_address'] = IRequest::get('work_address');
        $user_data['is_marry'] = IRequest::get('is_marry',IRequest::NOT_EMPTY,"婚姻状况不能为空！");
        $user_data['is_student'] = IRequest::get('is_student',IRequest::NOT_EMPTY,"是否是学生不能为空！");
        $user_data['is_real'] = IRequest::get('is_real');
        $user_data['intention_level'] = IRequest::get('intention_level',IRequest::NOT_EMPTY,"意向等级不能为空！");

        $identity = IRequest::get('identity');
        $sex = IRequest::get('sex');
        $age = IRequest::get('age');
        $voice_url = IRequest::get('voice_url');
        $proxy_url = IRequest::get('proxy_url');

        //取得其他参数
        $claim_data['money_start']= IRequest::get('min_loan',IRequest::NOT_EMPTY,"最小意向贷款不能为空！");
        $claim_data['money_end']= IRequest::get('max_loan',IRequest::NOT_EMPTY,"最大意向贷款不能为空！");
        $claim_data['period']= IRequest::get('period',IRequest::NOT_EMPTY,"周期不能为空！");
        $claim_data['user_note']= IRequest::get('user_note');

        $claim_data['money_start'] = floatval($claim_data['money_start']);
        $claim_data['money_end'] = floatval($claim_data['money_end']);
        $claim_data['period'] = intval($claim_data['period']);
        if($claim_data['period'] <= 0){
            error("贷款周期必须大于0！");
        }
        if(bccomp($claim_data['money_start'],$claim_data['money_end']) > 0){
            error("最小意向贷款不能大于最大意向贷款！");
        }

        if(!empty($voice_url)) $user_data['voice_url'] = $voice_url;
        if(!empty($proxy_url)) $user_data['proxy_url'] = $proxy_url;

        if(!empty($sex)) $user_data['sex'] = $sex;
        if(!empty($sex)) $user_data['sex'] = $sex;
        if(!empty($age)) $user_data['age'] = $age;
        if(!empty($identity)){
            $identity = trim($identity);
            $user_data['identity'] = $identity;

            //检查身份证号重复
            $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['identity'=>$user_data['identity']])->row();
            if($is_exist){
                //提示错误信息
                error("身份证号码已存在！");
            }

            //判断是否实名
            if($user_data['is_real'] != IsReal::yes){
                if(IJuHe::real($user_data['real'],$user_data['identity'])){
                    //实名成功，获取身份证号中的性别、年龄
                    $identity_info = IIdentity::getInfo($user_data['identity']);
                    $user_data['sex'] = $identity_info['sex'];
                    $user_data['age'] = $identity_info['age'];
                    //设置为已实名认证
                    $user_data['is_real'] = IsReal::yes;
                }else{
                    $user_data['is_real'] = IsReal::not;
                    error('实名认证失败，请检查姓名与身份证是否匹配');
                }
            }
        }else{
            $user_data['is_real'] = IsReal::not;
        }

        if($user_data['is_marry'] == IsMarry::married){
            //已婚
            $user_data['couple_name'] = IRequest::get('couple_name');
            $user_data['couple_phone'] = IRequest::get('couple_phone');
            $user_data['couple_identity'] = IRequest::get('couple_identity');
            $user_data['children_num'] = IRequest::get('children_num');
        }elseif ($user_data['is_marry'] == IsMarry::divorce){
            //离异
            $user_data['children_num'] = IRequest::get('children_num');
        }

        //职业值转化为字符串
        $user_data['occupation'] = HtmlEnumValue::$enum_value['identity'][$user_data['occupation']];

        //接收贷款数据
        $user_data['is_loan']= IRequest::get('is_loan',IRequest::NOT_EMPTY,"是否有贷款不能为空！");
        if($user_data['is_loan'] == IsLoan::Yes){
            $loan_data = IRequest::get('loan');
        }else{
            $loan_data = null;
        }

        //接收贷款数据
        $user_data['is_assure']= IRequest::get('is_assure',IRequest::NOT_EMPTY,"是否有担保不能为空！");
        if($user_data['is_assure'] == IsAssure::Yes){
            $assure_data = IRequest::get('assure');
        }else{
            $assure_data = null;
        }

        //接收公司数据
        $user_data['is_company']= IRequest::get('is_company',IRequest::NOT_EMPTY,"是否有公司不能为空！");
        if($user_data['is_company'] == IsCompany::Yes){
            $company_data = IRequest::get('company');
            if(empty($company_data['company_name'])){
                $company_data = null;
            }
        }else{
            $company_data = null;
        }

        //接收保单数据
        $user_data['is_policy']= IRequest::get('is_policy',IRequest::NOT_EMPTY,"是否有保单不能为空！");
        if($user_data['is_policy'] == IsPolicy::Yes){
            $policy_data = IRequest::get('policy');
        }else{
            $policy_data = null;
        }

        //接收信用卡数据
        $user_data['is_credit_card']= IRequest::get('is_card',IRequest::NOT_EMPTY,"是否有信用卡不能为空！");
        if($user_data['is_credit_card'] == IsCreditCard::Yes){
            $card_data = IRequest::get('card');
        }else{
            $card_data = null;
        }

        //接收房产数据
        $user_data['is_house']= IRequest::get('is_house',IRequest::NOT_EMPTY,"是否有房不能为空！");
        if($user_data['is_house'] == IsHouse::Yes){
            $house_data = IRequest::get('house');
        }else{
            $house_data = null;
        }

        //接收车辆数据
        $user_data['is_car']= IRequest::get('is_car',IRequest::NOT_EMPTY,"是否有车不能为空！");
        if($user_data['is_car'] == IsHouse::Yes){
            $car_data = IRequest::get('car');
        }else{
            $car_data = null;
        }

        //接收网店数据
        $user_data['is_shop']= IRequest::get('is_shop',IRequest::NOT_EMPTY,"是否有网店不能为空！");
        if($user_data['is_shop'] == IsShop::Yes){
            $shop_data = IRequest::get('shop');
        }else{
            $shop_data = null;
        }

        //整理区域数据
        $area = IRequest::get('area');
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }
        if(empty($province_id) || empty($city_id)){
            error("请选择客户所在城市！");
        }

        //设置数据
        $user_data['province_id'] = $province_id;
        $user_data['city_id'] = $city_id;

        //默认数据
        $user_data['agent_id'] = get_login_agent_id();
        $user_data['agent_admin_id'] = get_login_agent_admin_id();
        $user_data['reg_ip'] = get_client_ip();
        $user_data['source'] = Source::AgentAdmin;

        //检查手机重复
        $is_exist = IDb::getInstance('user')->setDbFiled('user_id')->setDbWhere(['phone'=>$user_data['phone']])->row();
        if($is_exist){
            //提示错误信息
            error("手机号码已存在！");
        }

        $user_data['nick'] = get_string_rsa($user_data['phone'], 3, 4) . "_" . rand(1000,9999);

        IDb::dbStartTrans();
        $user_id = IDb::getInstance('user')->setDbData($user_data)->add();
        if ($user_id === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("添加客户信息失败！");
        }

        //用户意向
        if(!empty($claim_data) && is_array($claim_data)){

            $result = \app\common\ext\User::addToViePool($user_id,$province_id,$city_id,$claim_data['money_start'],$claim_data['money_end'],$claim_data['period'],$user_data['intention_level']);
            if(!$result){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加贷款意向失败！");
            }
        }

        //公司信息
        if(!empty($company_data) && is_array($company_data)){
            $company_data['user_id'] = $user_id;
            $result = IDb::getInstance('user_company')->setDbData($company_data)->add();
            if(!$result){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加公司信息失败！");
            }
        }

        //贷款记录
        if(!empty($loan_data) && is_array($loan_data)){
            $result = true;
            foreach ($loan_data as $val){
                $val['user_id'] = $user_id;
                //添加贷款记录
                $result = IDb::getInstance('user_loan')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加贷款信息失败！");
            }
        }

        //担保记录
        if(!empty($assure_data) && is_array($assure_data)){
            $result = true;
            foreach ($assure_data as $val){
                $val['user_id'] = $user_id;
                //添加担保记录
                $result = IDb::getInstance('user_assure')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加担保信息失败！");
            }
        }

        //保单记录
        if(!empty($policy_data) && is_array($policy_data)){
            $result = true;
            foreach ($policy_data as $val){
                $val['user_id'] = $user_id;
                //添加保单记录
                $result = IDb::getInstance('user_policy')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加保单信息失败！");
            }
        }

        //信用卡记录
        if(!empty($card_data) && is_array($card_data)){
            $result = true;
            foreach ($card_data as $val){
                $val['user_id'] = $user_id;
                //添加信用卡记录
                $result = IDb::getInstance('user_credit_card')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加信用卡信息失败！");
            }
        }

        //房产记录
        if(!empty($house_data) && is_array($house_data)){
            $result = true;
            foreach ($house_data as $val){
                $val['user_id'] = $user_id;
                //添加房产记录
                $result = IDb::getInstance('user_house')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加房产信息失败！");
            }
        }

        //车辆记录
        if(!empty($car_data) && is_array($car_data)){
            $result = true;
            foreach ($car_data as $val){
                $val['user_id'] = $user_id;
                //添加车辆信息
                $result = IDb::getInstance('user_car')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加车辆信息失败！");
            }
        }

        //网店记录
        if(!empty($shop_data) && is_array($shop_data)){
            $result = true;
            foreach ($shop_data as $val){
                $val['user_id'] = $user_id;
                //添加网店信息
                $result = IDb::getInstance('user_shop')->setDbData($val)->add();
                if(!$result){
                    //事物回滚
                    IDb::dbRollback();
                    break;
                }
            }

            if(!$result){
                //提示错误信息
                error("添加网店信息失败！");
            }
        }

        //事物提交
        IDb::dbCommit();

        //设置日志
        IAgentTask::AgentMessage(get_login_agent_admin_id(),IAgentTask::UserInfo,array(array(2,IAgentTask::ToUser,$user_id)));

        //返回信息
        return $this->return_json(0,'添加成功');
    }
}
